﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SimpleNotify.Service.Contracts;
using System.ServiceModel.Description;
using System.IO;

namespace SimpleNotify.ConsoleService
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write(DateTime.Now.ToString() + " | Host Info | Starting Service ... ");

            IService serviceInstance = new Service.Service();
            string baseaddr = String.Format("http://{0}:80/Service.svc", Environment.MachineName);

            try
            {
                using (ServiceHost host = new ServiceHost(serviceInstance))
                {
                    try
                    {
                        WSDualHttpBinding binding = new WSDualHttpBinding(WSDualHttpSecurityMode.None);
                        binding.SendTimeout = new TimeSpan(0, 0, 3);
                        binding.OpenTimeout = new TimeSpan(0, 0, 20);
                        binding.CloseTimeout = new TimeSpan(0, 0, 20);
                        binding.ReliableSession.InactivityTimeout = new TimeSpan(0, 5, 0);
                        host.AddServiceEndpoint(typeof(IService), binding, baseaddr);

                        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                        smb.HttpGetEnabled = true;
                        smb.HttpGetUrl = new Uri(baseaddr);
                        host.Description.Behaviors.Add(smb);
                        
                        host.Open();
                        Console.WriteLine("OK");
                        WriteInfo("Server Name is: " + Environment.MachineName);
                        WriteInfo("waiting for subscribers ...");
                        Console.ReadLine();
                        host.Close();
                    }
                    catch (AddressAccessDeniedException ex1)
                    {
                        Console.WriteLine();
                        WriteError(ex1.Message);
                        Console.WriteLine();
                        WriteError("Use: netsh http add urlacl url=http://+:80/ user=DOMAIN\\UserName");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine();
                        WriteError(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                WriteError(ex.Message);
            }
            Console.ReadLine();
        }

        private static void WriteError(string text)
        {
            string textToLog = DateTime.Now.ToString() + " | Host Error | " + text;
            Console.WriteLine(textToLog);
            File.AppendAllText("SimpleNotify.Service.log", textToLog + Environment.NewLine);
        }

        private static void WriteInfo(string text)
        {
            string textToLog = DateTime.Now.ToString() + " | Host Info | " + text;
            Console.WriteLine(textToLog);
            File.AppendAllText("SimpleNotify.Service.log", textToLog + Environment.NewLine);
        }
    }
}
